/*
 * VideoMenuGameState.java
 *
 * Created on Jul 9, 2007, 7:04:14 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.gamestate;

import com.jme.system.DisplaySystem;
import com.jme.system.GameSettings;
import com.jmex.awt.swingui.JMEDesktopState;
import java.util.concurrent.Callable;
import com.jme.util.GameTaskQueueManager;
import java.awt.Font;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDesktopPane;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import kanjitori.KanjiTori;
import kanjitori.util.JMEDesktopUtil;
import kanjitori.util.VideoConstants;
import static kanjitori.util.Bundle.*;

/**
 *
 * @author DGronau
 */
public class VideoMenuGameState extends JMEDesktopState {

    private GameSettings gameSettings;

    private JComboBox resolutionBox;
    private JComboBox colorBox;
    private JComboBox frequencyBox;
    private JCheckBox fullscreenBox;
    
    public VideoMenuGameState(GameSettings gameSettings) {
        super(gameSettings.getWidth(), gameSettings.getHeight());
        this.gameSettings = gameSettings;
        GameTaskQueueManager.getManager().update(new Callable<Object>() {

            public Object call() throws Exception {
                initGraphics();
                return null;
            }
        });
    }

    public void initGraphics() {
        JDesktopPane jDesktop = getDesktop().getJDesktop();
        JMEDesktopUtil.init(jDesktop);

        Font font = new JLabel().getFont().deriveFont(Font.BOLD, 16f);

        JMEDesktopUtil.setJLabel(jDesktop, getString("menu_resolution"), new Point(-10, -100), font);
        JMEDesktopUtil.setJComboBox(jDesktop, resolutionBox = new JComboBox(), VideoConstants.RESOLUTIONS,
                new Point(10, -100));

        JMEDesktopUtil.setJLabel(jDesktop, getString("menu_colors"), new Point(-10, -50), font);
        JMEDesktopUtil.setJComboBox(jDesktop, colorBox = new JComboBox(), VideoConstants.COLORS, 
                new Point(10, -50));

        JMEDesktopUtil.setJLabel(jDesktop, getString("menu_frequency"), new Point(-10, 0), font);
        JMEDesktopUtil.setJComboBox(jDesktop, frequencyBox = new JComboBox(), VideoConstants.FREQUENCIES, 
                new Point(10, 0));

        JMEDesktopUtil.setJLabel(jDesktop, getString("menu_fullscreen"), new Point(-10, 50), font);
        fullscreenBox = new JCheckBox();
        jDesktop.add(fullscreenBox);
        JMEDesktopUtil.setSize(fullscreenBox);
        JMEDesktopUtil.pos(fullscreenBox, 10, 50, JMEDesktopUtil.H.LEFT, JMEDesktopUtil.V.CENTER);

        JButton okButton = new JButton(getString("menu_ok"));
        jDesktop.add(okButton);
        okButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                save();
            }
        });

        JButton cancelButton = new JButton(getString("menu_cancel"));
        jDesktop.add(cancelButton);
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                VideoMenuGameState.this.setActive(false);
                KanjiTori.setMenuGameState();
            }
        });

        JMEDesktopUtil.maxSize(okButton, cancelButton);
        JMEDesktopUtil.pos(okButton, -10, 120, JMEDesktopUtil.H.RIGHT, JMEDesktopUtil.V.CENTER);
        JMEDesktopUtil.pos(cancelButton, 10, 120, JMEDesktopUtil.H.LEFT, JMEDesktopUtil.V.CENTER);
    }

    public void reInit() {
        reInitJComboBox(resolutionBox, "" + gameSettings.getWidth() + "x" + gameSettings.getHeight());
        reInitJComboBox(colorBox, "" + gameSettings.getDepth());
        reInitJComboBox(frequencyBox, "" + gameSettings.getFrequency());
        fullscreenBox.setSelected(gameSettings.isFullscreen());
    }

    private void reInitJComboBox(JComboBox box, String value) {
        box.setSelectedItem(value);
        box.invalidate();
        box.validate();
        box.repaint();
    }

    private void save() {
        final JDesktopPane jDesktop = getDesktop().getJDesktop();
        int width = VideoConstants.WIDTH[resolutionBox.getSelectedIndex()];
        int height = VideoConstants.HEIGHTS[resolutionBox.getSelectedIndex()];
        int depth = Integer.parseInt(VideoConstants.COLORS[colorBox.getSelectedIndex()]);
        int freq = Integer.parseInt(VideoConstants.FREQUENCIES[frequencyBox.getSelectedIndex()]);
        if (!DisplaySystem.getDisplaySystem().isValidDisplayMode(width, height, depth, freq)) {
            JMEDesktopUtil.showMessageDialog(getDesktop(), getString("menu_videoError"), 
                    getString("menu_wrongMode"), JOptionPane.ERROR_MESSAGE);
        } else {
            VideoMenuGameState.this.setActive(false);
            gameSettings.setWidth(width);
            gameSettings.setHeight(height);
            gameSettings.setDepth(depth);
            gameSettings.setFrequency(freq);
            gameSettings.setFullscreen(fullscreenBox.isSelected());
            KanjiTori.savePreferences();
            KanjiTori.changeResolution();
            KanjiTori.setMenuGameState();
        }
    }
}
